/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax;

import deatax.unit.Unit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

/**
 *
 * @author pson
 */
public class GameManager {
    
    private Settlement base_settlement = null;
    private ArrayList<Resource> resource_list = null;
    private LinkedHashMap<String, Resource> resources = new LinkedHashMap<String, Resource>();
    private ArrayList<Improvement> improve_list = null;
    private LinkedHashMap<String, Improvement> improves = new LinkedHashMap<String, Improvement>();
    private ArrayList<Terrain> terrain_list = null;
    private LinkedHashMap<String, Terrain> terrains = new LinkedHashMap<String, Terrain>();
    private ArrayList<Landmass> landmass_list = null;
    private LinkedHashMap<String, Landmass> landmasses = new LinkedHashMap<String, Landmass>();
    private ArrayList<Unit> unit_list = null;
    private LinkedHashMap<String, Unit> units = new LinkedHashMap<String, Unit>();
    
    
    
    public void calculate_bases() {
        int min_level = 9999;
        for (Entry<String, Improvement> e : improves.entrySet()) {
            if (e.getValue() instanceof Settlement) {
                Settlement s = (Settlement)e.getValue();
                if (s.getLevel() < min_level) {
                    this.base_settlement = s;
                    min_level = s.getLevel();
                }
            }
        }
    }
    
    public ArrayList<Improvement> getAllImproves() {
        if (improve_list == null) {
            improve_list = new ArrayList<Improvement>();
            for (Entry<String, Improvement> e : improves.entrySet()) {
                improve_list.add(e.getValue());
            }
        }
        return improve_list;
    }
    
    public Improvement getImprove(String s) {
        return improves.get(s);
    }
    
    public void addImprove(String s, Improvement i) {
        improves.put(s, i);
    }

    public void setImproves(LinkedHashMap<String, Improvement> improves) {
        this.improves = improves;
    }

    public void setLandmasses(LinkedHashMap<String, Landmass> landmasses) {
        this.landmasses = landmasses;
    }
    
    public Landmass getLandmass(String s) {
        return landmasses.get(s);
    }
    
    public void addLandmass(String s, Landmass l) {
        landmasses.put(s, l);
    }
    
    public ArrayList<Resource> getAllResources() {
        if (resource_list == null) {
            resource_list = new ArrayList<Resource>();
            for (Entry<String, Resource> e : resources.entrySet()) {
                resource_list.add(e.getValue());
            }
        }
        return resource_list;
    }

    public ArrayList<Resource> getTaggedResources(String tag) {
        ArrayList<Resource> full_list = getAllResources();
        ArrayList<Resource> tag_list = new ArrayList<Resource>();
        for (Resource r : full_list) {
            if (r.hasTag(tag)) {
                tag_list.add(r);
            }
        }
        return tag_list;
    }

    public void setResources(LinkedHashMap<String, Resource> resources) {
        this.resources = resources;
    }
    
    public Resource getResource(String s) {
        return resources.get(s);
    }
    
    
    
    public void addResource(String s, Resource r) {
        resources.put(s, r);
    }

    public ArrayList<Terrain> getAllTerrains() {
        if (terrain_list == null) {
            terrain_list = new ArrayList<Terrain>();
            for (Entry<String, Terrain> e : terrains.entrySet()) {
                terrain_list.add(e.getValue());
            }
        }
        return terrain_list;
    }
    

    public void setTerrains(LinkedHashMap<String, Terrain> terrains) {
        this.terrains = terrains;
    }
    
    public Terrain getTerrain(String s) {
        return terrains.get(s);
    }
    
    public void addTerrain(String s, Terrain t) {
        terrains.put(s, t);
    }
    
    
    public ArrayList<Unit> getAllUnits() {
        if (unit_list == null) {
            unit_list = new ArrayList<Unit>();
            for (Entry<String, Unit> e : units.entrySet()) {
                unit_list.add(e.getValue());
            }
        }
        return unit_list;
    }
    

    public void setUnits(LinkedHashMap<String, Unit> units) {
        this.units = units;
    }
    
    public Unit getUnit(String s) {
        return units.get(s);
    }
    
    public void addUnit(String s, Unit u) {
        units.put(s, u);
    }

    public Settlement getBaseSettlement() {
        return this.base_settlement;
    }
}
